/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.shrikanthravi.customnavigationdrawer2.utils;


import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * ResourceUtil
 *
 * @author wangyin
 * @since 2021/06/23
 */
public class ResourceUtil {
    /**
     * Context
     */
    private final Context mContext;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public ResourceUtil(Context context) {
        this.mContext = context;
    }

    /**
     * 设置背景图片
     *
     * @param res 图片资源
     * @return Element
     */
    public Element getImageDrawable(int res) {
        PixelMapElement drawable = null;
        try {
            drawable = new PixelMapElement(
                    mContext.getResourceManager().getResource(res));
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
        return drawable;
    }

    /**
     * 通过颜色id获取element对象
     *
     * @param res res
     * @return Element
     */
    public Element getColorDrawable(int res) {
        int color = getColor(res);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(color));

        return shapeElement;
    }

    /**
     * 获取颜色
     *
     * @param res res
     * @return int
     */
    public int getColor(int res) {
        int result = 0;
        try {
            result = mContext.getResourceManager().getElement(res).getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        return result;
    }
}
